﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rotempco.ORM.Generator.Objects;
using System.IO;
using Rotempco.ORM.Generator.Enums;

namespace Rotempco.ORM.Generator.Logic
{
    public class ProjectFileLogic
    {
        public static void InsertAndSaveElementToProject(RotempcoSettings projectSettings, string projectPath, string codeFileName, string dependentsUpon = null)
        {
            string projectFilePath = projectPath + projectSettings.GetValue("@MainProjectFile");

            string projectFileContent = InsertElementToProject(projectSettings, File.ReadAllText(projectFilePath), projectPath, codeFileName, dependentsUpon);

            File.WriteAllText(projectFilePath, projectFileContent);
        }

        public static string InsertElementToProject(RotempcoSettings projectSettings, string projectsFileContent, string projectPath, string codeFileName, string dependentsUpon = null)
        {
            int ormIndex = projectsFileContent.LastIndexOf("<!--ORM-->");

            if (ormIndex <= -1)
            {
                projectsFileContent = projectsFileContent.Insert(
                     projectsFileContent.LastIndexOf("</Project>") - 1,
                     @"<!--ORM-->
    <ItemGroup>
    </ItemGroup>");

                ormIndex = projectsFileContent.LastIndexOf("<!--ORM-->");
            }

            int itemGroupIndex = projectsFileContent.IndexOf("</ItemGroup>", ormIndex) - 1;

            string compileIncludeName = "<Compile Include=\"" + codeFileName + "\"";
            if (projectsFileContent.ToLower().Contains(compileIncludeName.ToLower()))
            {
                // Do Nothing
            }
            else
            {
                string compileInclude = @"
    <Compile Include=""" + codeFileName + @""">";

                if (dependentsUpon != null)
                {
                    compileInclude += @"
        <DependentUpon>" + dependentsUpon + "</DependentUpon>";
                }
                compileInclude += @"
        <SubType>Code</SubType>
    </Compile>
";

                projectsFileContent = projectsFileContent.Insert(itemGroupIndex, compileInclude);
            }

            return projectsFileContent;
        }

        public static void UpdateProjectsFile(RotempcoSettings projectSettings, string projectPath, string className)
        {
            string projectsFileContent = File.ReadAllText(projectPath + projectSettings.GetValue("@MainProjectFile"));
            string projectNamespace = FileNameLogic.GetProjectNamespace(projectSettings);

            string dalNamespace = projectSettings.GetValue("@DALImplementationNamespace").Replace(projectNamespace + ".", "").Replace(".", "\\") + "\\";
            string entityObjectNamespace = projectSettings.GetValue("@EntityObjectNameSpace").Replace(projectNamespace + ".", "").Replace(".", "\\") + "\\";
            string propertyEntityObjectTemplateNameSpace = projectSettings.GetValue("@PropertyEntityObjectTemplateNameSpace").Replace(projectNamespace + ".", "").Replace(".", "\\") + "\\";

            projectsFileContent = InsertElementToProject(projectSettings, projectsFileContent, projectPath, dalNamespace + FileNameLogic.GetClassFileName(className, TemplateEnum.DAL));
            projectsFileContent = InsertElementToProject(projectSettings, projectsFileContent, projectPath, dalNamespace + FileNameLogic.GetClassFileName(className, TemplateEnum.ADALCodebehind), FileNameLogic.GetClassFileName(className, TemplateEnum.DAL));
            projectsFileContent = InsertElementToProject(projectSettings, projectsFileContent, projectPath, entityObjectNamespace + FileNameLogic.GetClassFileName(className, TemplateEnum.EntityObject));
            projectsFileContent = InsertElementToProject(projectSettings, projectsFileContent, projectPath, propertyEntityObjectTemplateNameSpace + FileNameLogic.GetClassFileName(className, TemplateEnum.PropertyEntityObject));

            projectsFileContent = InsertElementToProject(projectSettings, projectsFileContent, projectPath, "Factory.cs");
            projectsFileContent = InsertElementToProject(projectSettings, projectsFileContent, projectPath, "DataFactory.cs");

            File.WriteAllText(projectPath + projectSettings.GetValue("@MainProjectFile"), projectsFileContent);
        }
    }
}
